package study.saas.web.portal.sys.admin.Controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import study.saas.common.dto.CommodityPageDTO;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageResult;
import study.saas.order.entity.Commodity;
import study.saas.order.service.CommodityService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("sys/commodity")
@Slf4j
@Api(tags = "商品管理接口")
public class CommodityController {
    @Resource
    CommodityService commodityService;

    //功能一：新增商品
    @PostMapping("/add")
    @ApiOperation("新增商品")
    public ApiResultV2 add (@RequestBody Commodity commodity) {
        log.info("新增商品:{}", commodity);
        commodityService.add(commodity);
        return ApiResultV2.ok();
    }

    //功能二：商品修改
    @PutMapping("/update")
    @ApiOperation("修改商品")
    public ApiResultV2 update (@RequestBody Commodity commodity) {
        log.info("修改商品:{}", commodity);
        commodityService.update(commodity);
        return ApiResultV2.ok();
    }

//    功能三-一：根据id查询商品
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商品")
    public ApiResultV2<Commodity> getById(@PathVariable Integer id) {
        log.info("根据id查询商品:{}",id);
        Commodity commodity = commodityService.getById(id);
        return ApiResultV2.ok(commodity);
    }

    //功能三-二 根据商品名称查询商品
    @GetMapping("/get")
    @ApiOperation("根据商品名称查询商品")
    public ApiResultV2 getByName (@RequestParam String commodityName) {
        log.info("根据商品名称查询商品:{}", commodityName);
        Commodity commodity = commodityService.getByName(commodityName);
        return ApiResultV2.ok(commodity);
    }

//    //功能四-MyBatis：分页查询商品
//    @GetMapping("/page")
//    @ApiOperation("分页查询商品")
//    public ApiResultV2<PageResult> getByPage(CommodityPageDTO commodityPageDTO) {
//    log.info("分页查询商品:{}",commodityPageDTO.toString());
//    PageResult pageResult = commodityService.getByPage(commodityPageDTO);
//    return ApiResultV2.ok(pageResult);
//    }

    //功能四-JPA：分页查询商品
    @GetMapping("/pagequary")
    @ApiOperation("利用JPA分页查询商品")
    public ApiResultV2<PageResult> getByPageByJpa(CommodityPageDTO commodityPageDTO) {
        return ApiResultV2.ok(commodityService.getByPageByJpa(commodityPageDTO));
    }


//    //功能五：上架下架商品
//    @PostMapping("/status/{status}")
//    @ApiOperation("起售停售商品")
//    public ApiResultV2 updateStatus(@PathVariable Integer status,Integer id) {
//        log.info("起售停售商品:{}:{}",status,id);
//        commodityService.updateStatus(status,id);
//        return ApiResultV2.ok();
//    }



    //功能六:修改商品库存
    @PostMapping("/amount/{amount}")
    @ApiOperation("修改商品库存")
    public ApiResultV2 updateAmount(@PathVariable Integer amount,Integer id) {
        log.info("修改商品库存:{}:{}",amount,id);
        commodityService.updateAmount(amount,id);
        return ApiResultV2.ok();
    }

    //功能七：删除商品
    @DeleteMapping("/delete")
    @ApiOperation("删除商品")
    public ApiResultV2 delete(@RequestParam List<Integer> ids) {
        log.info("批量删除商品");
        commodityService.deleteByIds(ids);
        return ApiResultV2.ok();
    }

    //功能七-二：根据商品名称删除商品
    @DeleteMapping("/deleteByName")
    @ApiOperation("根据商品名称删除商品")
    public ApiResultV2 deleteByName(@RequestParam String commodityName) {
        commodityService.deleteByName(commodityName);
        return ApiResultV2.ok();
    }

}
